package com.comba.javafxdemo.controller;

import java.net.URL;
import java.text.DateFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import com.comba.javafxdemo.model.Person;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.XYChart;

public class BirthdayStatisticsController implements Initializable {
	@FXML
	private BarChart<String, Integer> barChart;
	@FXML
	private CategoryAxis xAxis;
	private ObservableList<String> monthNames = FXCollections.observableArrayList();

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		String[] months = DateFormatSymbols.getInstance(Locale.ENGLISH).getMonths();
		this.monthNames.addAll(months);
		this.xAxis.setCategories(monthNames);
	}

	public void setPersonData(List<Person> persons) {
		int [] monthCounter = new int[12];
		for(Person p : persons) {
			int month = p.getBirthday().getMonthValue() - 1;
			monthCounter[month]++;
		}

		XYChart.Series<String, Integer> series = new XYChart.Series<>();
		for(int i=0; i< monthCounter.length; i++) {
			series.getData().add(new XYChart.Data<String, Integer>(monthNames.get(i), monthCounter[i]));
		}

		barChart.getData().add(series);
	}
}
